package com.isaac.service;

import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.InvokerListener;
import org.apache.dubbo.rpc.RpcException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class printCostTimeThread extends Thread {

    Map<String, List<Integer>> recordMap;


    public printCostTimeThread(Map<String, List<Integer>> recordMap) {
        this.recordMap = recordMap;
    }

    @Override
    public void run() {
        System.out.println();
        System.out.println("---------开始每5s打印接口监控--------");
        // 2.每5秒打印一次耗时情况
        // 打印Map中每个方耗时
        for (Map.Entry<String, List<Integer>> entry : recordMap.entrySet()) {
            final String methodName = entry.getKey();
            int totalCounts = entry.getValue().size();
            System.out.println("Method: " + methodName + ",totalCounts: " + totalCounts);

            int top90Index = (int) (Math.ceil(totalCounts * 0.9D) - 1);
            int top99Index = (int) (Math.ceil(totalCounts * 0.99D) - 1);
            System.out.println("Method: " + methodName + ",[top90Index: " + top90Index + ",top99Index: " + top99Index + "]");

            entry.getValue().sort(Integer::compareTo);
            System.out.println("Method: " + methodName + ",[top90 = " + entry.getValue().get(top90Index) + ", top99 = " + entry.getValue().get(top99Index) + "]");
        }
        System.out.println("---------结束每5s打印接口监控--------");
        System.out.println();
    }
}
